-
-
Notifications
You must be signed in to change notification settings - Fork 141
Infer dtype of Series in more cases #766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I don't know why both mypy and pyright think that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw this is in draft form, but I think my comments would need to be addressed anyway
assert assert_type(s.replace(1, 2, inplace=True), None) is None | ||
|
||
|
||
def test_cat_accessor() -> None: | ||
# GH 43 | ||
s = pd.Series(pd.Categorical(["a", "b", "a"], categories=["a", "b"])) | ||
s: pd.Series[str] = pd.Series( | ||
pd.Categorical(["a", "b", "a"], categories=["a", "b"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should have a CategoricalSeries
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we might need to reconsider S1 at some point (not in this PR). I would slightly prefer S1 = TypeVar("S1")
as any type can be in a Series.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but I'd rather encourage "typical" types to be in a Series
, and let people do cast
or ignore
if they are putting other types in a Series
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could do a CategoricalSeries
as a future PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It all looks good. Some open comments to address. I think this is the list:
- Where you changed the construction of the
Series
to shorten them, I'd rather leave them as is to have less changes to the tests. - For
Series.update()
, need to surround thatignore
with aTYPE_CHECKING_INVALID_USAGE
pattern - Use
np.integer
rather thannp.intp
- For strings passed into
Series.agg()
, maybe we just need to have an overload fornp.mean
and"mean"
to say they returnSeries[float]
(as opposed to what I wrote elsewhere about returningAny
) and then you can remove theignore
in the tests.
I believe I fixed everything but agg/aggregate. will look into whether we can do something like:
|
pandas-stubs/core/series.pyi
Outdated
@@ -834,7 +859,7 @@ class Series(IndexOpsMixin[S1], NDFrame): | |||
axis: AxisIndex = ..., | |||
*args, | |||
**kwargs, | |||
) -> Series[S1]: ... | |||
) -> Self: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe make this Series
since we really don't know the type of the Series
- could be mix of int and float, or one, or the other
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, that made it work!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty close. Just that one case where there is a mix of funcs on the Series.agg()
call. Maybe the solution is that if a list is passed, the result is Series
instead of Series[S1]
. See comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @twoertwein
assert_type()
to assert the type of any return value